package com.biostime.qdingding.modules.qd.service;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.modules.qd.api.dto.CommentDto;
import com.biostime.qdingding.modules.qd.dao.CommentDao;
import com.biostime.qdingding.modules.qd.entity.Comment;
import com.biostime.qdingding.modules.sys.entity.User;

/**
 * 评论Service
 * @author king
 * @version 2015-11-05
 */
@Service
@Transactional(readOnly = true)
public class CommentService extends CrudService<CommentDao, Comment> {
	@Autowired
	CommentDao commentDao;

	@Override
	public Page<Comment> findPage(Page<Comment> page, Comment entity) {
		entity.setPage(page);
		page.setList(this.findList(entity));
		return page;
	}
	
	@Override
	public List<Comment> findList(Comment entity) {
		dataScopeFilter(entity, "dsf", "id=b.center_id", "id=b.create_by");
		
		return dao.findList(entity);
	}
	
//	 <!-- ————————————————————————————楚河汉界，谁敢动我的，打死! ————————————————————————————————————-->		
	public int apiInsert(Comment comment){
		comment.setCreateBy(new User("0"));
		comment.setCreateDate(new Date());
		comment.setUpdateBy(new User("0"));
		comment.setUpdateDate(new Date());
		comment.setDelFlag("0");
		comment.setIsAudit(0);		
		return commentDao.apiInsert(comment);
	}
	
	public Comment apiGetComment(Comment comment){
		return commentDao.apiGetComment(comment);
	}
	
	public HashMap<String,Object> apiScoreByTheme(String serialId,String levelId){
		return commentDao.apiScoreByTheme(serialId, levelId);
	}
	
	public List<CommentDto> apiGetCommentList(String serialId,String levelId,String userId){
		return commentDao.apiGetCommentList(serialId,levelId,userId); 
	}
	
	public List<CommentDto> apiGetNewComments(String centerId,String userId){
		return commentDao.apiGetNewComments(centerId,userId);
	}
	
	public List<String> apiGetHeadImage(String serialId,String levelId){
		return dao.apiGetHeadImage(serialId, levelId);
	}
	
	public int apiGetCommentNum(String objectId,String category){
		return dao.apiGetCommentNum(objectId, category);
	}
	
	public List<CommentDto> apiActivityComments(String activityId,String userId){
		return dao.apiActivityComments(activityId,userId);
	}
	
	
	/**
	 * 获取客户端真实IP地址
	 * @param request
	 * @return
	 */
	 public String getIpAddress(HttpServletRequest request) { 
		    String ip = request.getHeader("x-forwarded-for"); 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("Proxy-Client-IP"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("WL-Proxy-Client-IP"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("HTTP_CLIENT_IP"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 
		    } 
		    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
		      ip = request.getRemoteAddr(); 
		    } 
		    return ip; 
	 } 
	 
	/**
	 * 获取本地真实IP地址
	 * @param request
	 * @return
	 */	 
    public InetAddress getAddress() {
        try {
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                if (addresses.hasMoreElements()) {
                    return addresses.nextElement();
                }
            }
        } catch (SocketException e) {	
            //Test.logger.info(("获取ip地址出现错误 <{}>."+e.getMessage()));
        }
        return null;
    }

	public boolean audit(Comment comment) {
		return dao.audit(comment);
	}	 
	 
}

